package com.qiwo.webshow.controller;


import java.util.Arrays;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qiwo.common.base.AdminBaseController;
import com.qiwo.webshow.domain.DesignerDO;
import com.qiwo.webshow.service.DesignerService;
import com.qiwo.common.utils.Result;

/**
 * 
 * <pre>
 * 设计师图片管理
 *  @author  chenwenchao
 *  @date 2018-10-25 20:19:42
 * </pre>
 * <small> 2018-10-25 20:19:42 | chenwenchao</small>
 */
@Controller
@RequestMapping("/webshow/designer")
public class DesignerController extends AdminBaseController {
	@Autowired
	private DesignerService designerService;
	
	@GetMapping()
	@RequiresPermissions("webshow:designer:designer")
	String Designer(){
	    return "webshow/designer/designer";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("webshow:designer:designer")
	public Result<Page<DesignerDO>> list(DesignerDO designerDTO){
        Wrapper<DesignerDO> wrapper = new EntityWrapper<DesignerDO>().orderBy("id", false);
        if(designerDTO!=null){
        	if(designerDTO.getTitle()!=null){
        		wrapper.like("title",designerDTO.getTitle());
			}
		}
        Page<DesignerDO> page = designerService.selectPage(getPage(DesignerDO.class), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("webshow:designer:add")
	String add(){
	    return "webshow/designer/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("webshow:designer:edit")
	String edit(@PathVariable("id") Long id,Model model){
		DesignerDO designer = designerService.selectById(id);
		model.addAttribute("designer", designer);
	    return "webshow/designer/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("webshow:designer:add")
	public Result<String> save( DesignerDO designer){
		designerService.insert(designer);
        return Result.ok();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("webshow:designer:edit")
	public Result<String>  update( DesignerDO designer){
		boolean update = designerService.updateById(designer);
		return update ? Result.ok() : Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("webshow:designer:remove")
	public Result<String>  remove( Long id){
		designerService.deleteById(id);
        return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("webshow:designer:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		designerService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
